var mongoose = require('../../../../../framework/mongoose');
var service = require('./service');

var CmsEvent = require('./model').CmsEvent;

//Rest Interface
exports.queryEventBySubId = function (req, res) {
    CmsEvent.find({subscription: req.query.subId}).sort('order').exec(function (err, events) {
        res.send({code: 1, data: {events: events}});
    });
};
exports.findById = function(req, res){
    mongoose.findById(CmsEvent, req.params.id, function(obj) {res.send({event: obj});});
};

exports.save = function (req, res) {
    var cmsEvent = new CmsEvent({
        title: req.body.title,
        descr: req.body.descr,
        date: req.body.date,
        image: req.body.image,
        createDate: new Date(),
        subscription: req.body.subId
    });
    cmsEvent.save(function(){
        return res.send({code: 1});
    });
};

exports.update = function(req, res){
    CmsEvent.findByIdAndUpdate(
        req.params.id,
        {
            title: req.body.title,
            descr: req.body.descr,
            date: req.body.date,
            image: req.body.image,
            updateDate: new Date()
        }, function(){
            return res.send({code: 1});
        }
    )
};

exports.delete = function(req, res){
    CmsEvent.findByIdAndRemove(req.params.id, function(){
        return res.send({});
    })
};